package cac.fenjian.resource.mstaskpackagebasic.service;

import cac.common.DateUtil;
import cac.fenjian.resource.AoTask.MsTaskPackage.dto.MsTaskPackageDTO;
import cac.fenjian.resource.mstaskpackagebasic.dao.MsTaskPackageBasicDao;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.utils.ComUtil;
import com.cac.dme.adapter.utils.PageHelper;
import com.cac.dme.adapter.utils.PojoUtil;
import com.cac.dme.common.exception.service.DaoException;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MsTaskPackageBasicService {


    @Autowired
    private MsTaskPackageBasicDao msTaskPackageBasicDao;


    Date date = new Date();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = sf.format(date);
    Date time = sf.parse(nowTime);

    public MsTaskPackageBasicService() throws ParseException {
    }

    /*
    条件查询
    */
    @Transactional(readOnly = true)
    public MsTaskPackageDTO selectById(String msTaskPackageId) {
        try {
            MsTaskPackageDTO MsTaskPackageDTO = msTaskPackageBasicDao.selectById(msTaskPackageId);
            if (MsTaskPackageDTO != null) {
            }
            return MsTaskPackageDTO;

        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //分页查询
    @Transactional(readOnly = true)
    public QueryRespBean<MsTaskPackageDTO> searchBasicByPage(QueryReqBean<MsTaskPackageDTO> queryReqBean, String shelvesCode, String orderBy) {
        QueryRespBean<MsTaskPackageDTO> queryRespBean = new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            MsTaskPackageDTO searchParams = queryReqBean.getSearchParams();
            Page<MsTaskPackageDTO> dataList = msTaskPackageBasicDao.searchBasicByPage(searchParams, shelvesCode,  queryReqBean.getKeyWord());
            queryRespBean.setResult(dataList);
            return queryRespBean;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //单条插入
    @Transactional
    public String insertBasic(MsTaskPackageDTO MsTaskPackageDTO){
        try {
            MsTaskPackageDTO.setMsTaskPackageId(ComUtil.getId());
            MsTaskPackageDTO.setCreationDate(DateUtil.nowTime());
            msTaskPackageBasicDao.insertMsTaskPackageDTO(MsTaskPackageDTO);
            return MsTaskPackageDTO.getMsTaskPackageId();
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }

    //批量新增
    @Transactional
    public int insertBasicList(List<MsTaskPackageDTO> dtoList) {
        List<MsTaskPackageDTO> beanList = new ArrayList<>();
        for (MsTaskPackageDTO MsTaskPackageDTO : dtoList) {
            MsTaskPackageDTO.setMsTaskPackageId(ComUtil.getId());
            beanList.add(MsTaskPackageDTO);
        }
        try {
            return msTaskPackageBasicDao.insertBasicList(beanList);
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }


    //单条更新
    @Transactional
    public int updateBasic(MsTaskPackageDTO MsTaskPackageDTO) throws ParseException {
        MsTaskPackageDTO.setLastUpdateDate(DateUtil.nowTime());
        try {
            int count = msTaskPackageBasicDao.updateBasic(getUpdateDto(MsTaskPackageDTO));
            if(count==0){
                throw new DaoException("数据失效，请重新更新");
            }
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }

    //批量更新
    @Transactional
    public  int updateList(List<MsTaskPackageDTO> dtoList){
        List<MsTaskPackageDTO> beanList =new ArrayList<>();
        for(MsTaskPackageDTO MsTaskPackageDTO: dtoList){
            MsTaskPackageDTO oldDTO =getUpdateDto(MsTaskPackageDTO);
            beanList.add(oldDTO);
        }
        try {
            return msTaskPackageBasicDao.updateBasicList(beanList);
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }

    //批量更新2
    @Transactional
    public int updateBasicList2(List<MsTaskPackageDTO> dtoList){
        int count =0;
        for (MsTaskPackageDTO msDTO:dtoList
        ) {
            count =msTaskPackageBasicDao.updateBasic(msDTO);
        }
        return count;
    }


    //自定义方法，获取需要修改的dto对象
    private MsTaskPackageDTO getUpdateDto(MsTaskPackageDTO MsTaskPackageDTO){
        MsTaskPackageDTO oldDto = findById(MsTaskPackageDTO.getMsTaskPackageId());
        if(oldDto == null ){
            throw new DaoException("数据不存在");
        }
        PojoUtil.copyProperties(oldDto,MsTaskPackageDTO,true);
        return oldDto;
    }


    private MsTaskPackageDTO findById(String msPartId){
        return msTaskPackageBasicDao.selectById(msPartId);
    }


    //删除
    @Transactional
    public int deleteBasic(String msTaskPackageId) {
        if (StringUtils.isEmpty(msTaskPackageId)) {
            throw new DaoException("删除失败！传入的参数主键为null");
        }
        try {
            //记录日志
            MsTaskPackageDTO MsTaskPackageDTO = findById(msTaskPackageId);
            if (MsTaskPackageDTO == null) {
                throw new DaoException("删除失败！对象不存在");
            }
            //删除业务数据
            int count = msTaskPackageBasicDao.deleteBasic(msTaskPackageId);
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //批量删除
    @Transactional
    public int deleteBasicByIds(String[] ids) {
        int result = 0;
        for (String id : ids) {
            deleteBasic(id);
            result++;
        }
        return result;
    }
}
